package com.ilink.groundservice.controller.AiX;

import com.ilink.ilinkapi.dto.ground.AiX.AiXAreaFilterRequest;
import com.ilink.ilinkapi.dto.ground.AiX.Course.AiXCourseInsertRequest;
import com.ilink.ilinkapi.dto.ground.AiX.Course.AiXCourseUpdateRequest;
import com.ilink.ilinkcommon.domain.User;
import com.ilink.ilinkcommon.log.method.MethodLog;
import java.util.Arrays;
import java.util.List;

import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ilink.groundservice.entity.VO.AiX.AiXCourseResponse;
import com.ilink.groundservice.service.AiX.AiXCourseService;
import com.ilink.ilinkcommon.domain.R;
import org.springframework.http.HttpStatus;
import com.ilink.ilinkcommon.utils.BeanUtils;
import com.ilink.ilinkcommon.utils.UserContext;
import com.ilink.ilinkapi.client.UserServiceClient;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.multipart.MultipartFile;

@Slf4j
@RequestMapping("/ground/aix/course")
@RestController
@RequiredArgsConstructor
public class AiXCourseController {

  private final AiXCourseService aiXCourseService;
  private final UserServiceClient userServiceClient;

  @MethodLog("分页返回课程元数据")
  @GetMapping("/get_courses")
  public R getCourses(AiXAreaFilterRequest request) {
    try {
      IPage<AiXCourseResponse> responseIPage = aiXCourseService.getCourses(request);
      if (responseIPage != null) {
        return R.ok("分页返回课程元数据", responseIPage);
      } else {
        return R.error("查询课程数据失败");
      }
    } catch (Exception e) {
      log.error(e.getMessage());
      return R.error(e.getMessage());
    }
  }

  @MethodLog("插入课程元数据")
  @PostMapping("/insert_course")
  public R insertCourse(AiXCourseInsertRequest request) {
    if (!isTeacher(UserContext.getUserId())) {
      return R.error("无教师权限");
    }
    try {
      aiXCourseService.insertCourse(request);
      return R.ok("课程插入成功");
    } catch (Exception e) {
      log.error(e.getMessage());
      return R.error(e.getMessage());
    }
  }

  @MethodLog("更新课程数据")
  @PostMapping("/update_course")
  public R updateCourse(@RequestBody AiXCourseUpdateRequest request) {
    if (!isTeacher(UserContext.getUserId())) {
      return R.error("无教师权限");
    }
    try {
      aiXCourseService.updateCourse(request);
      return R.ok("课程更新成功");
    } catch (Exception e) {
      log.error(e.getMessage());
      return R.error(e.getMessage());
    }
  }

  @MethodLog("更新课程封面图")
  @PostMapping("/update_course_cover")
  public R updateCourseCover(MultipartFile file, Long courseId, Long areaId) {
    if (!isTeacher(UserContext.getUserId())) {
      return R.error("无教师权限");
    }
    try {
      aiXCourseService.updateCourseCover(file, courseId, areaId);
      return R.ok("课程封面图更新成功");
    } catch (Exception e) {
      log.error(e.getMessage());
      return R.error(e.getMessage());
    }
  }

  @MethodLog("删除课程元数据")
  @PostMapping("/delete_course/{courseId}")
  public R deleteCourse(@PathVariable Long courseId) {
    if (!isTeacher(UserContext.getUserId())) {
      return R.error("无教师权限");
    }
    try {
      aiXCourseService.deleteCourse(courseId);
      return R.ok("课程删除成功");
    } catch (Exception e) {
      log.error(e.getMessage());
      return R.error(e.getMessage());
    }
  }

  @MethodLog("获取单个课程")
  @GetMapping("/get_course/{courseId}")
  public R getCourse(@PathVariable Long courseId) {
    try {
      AiXCourseResponse response = aiXCourseService.getCourse(courseId);
      return R.ok("课程获取成功", response);
    } catch (Exception e) {
      log.error(e.getMessage());
      return R.error(e.getMessage());
    }
  }

  @MethodLog("获取课程最常用的8个标签")
  @GetMapping("/get_course_tags")
  public R getCourseTags(@RequestParam Long areaId) {
    try {
      List<String> tags = aiXCourseService.getCourseTags(areaId);
      return R.ok("课程最常用的8个标签", tags);
    } catch (Exception e) {
      log.error(e.getMessage());
      return R.error(e.getMessage());
    }
  }

  private boolean isTeacher(String userId) {
    R whoami = userServiceClient.whoami(userId);
    HttpStatus status = HttpStatus.valueOf(whoami.getCode());
    if (!status.is2xxSuccessful()) {
      log.error(String.format("获取用户信息失败，用户ID: %s", userId));
      return false;
    }
    User userDetail = BeanUtils.copyBean(whoami.getData(), User.class);
    String userType = userDetail.getUserType();
    return Arrays.asList("BIAdmin", "L0101", "L0107", "L0108", "L0109").contains(userType);
  }
}
